package com.wx.chapter8;

/**
 * ClassName:HomeWork01
 * Package :com.wx.chapter8
 * Description :
 *
 * @Author : coder_mu
 * @Create : 2023/5/28 - 14:59
 */
public class HomeWork01 {
    public static void main(String[] args) {
        Person p1 = new Person("jay1", 19, "搬砖");
        Person p2 = new Person("jay2", 18, "盖房子");
        Person p3 = new Person("jay3", 20, "吃播");
        Person[] peoples = {p1, p2, p3};//创建一个Person类型的数组进行对象存储，类似于向下转型，使用getAge方法
        Person temp = null;//用于交换的中间变量

        for (int i = 0; i < peoples.length - 1; i++) {//冒泡排序
            for (int j = 0; j < peoples.length - 1 - i; j++) {
                if (peoples[j].getAge() > peoples[j + 1].getAge()) {
                    temp = peoples[j];
                    peoples[j] = peoples[j + 1];
                    peoples[j + 1] = temp;
                }
            }
        }
        for (Person people : peoples) {
            System.out.println(people);
        }
    }
}

class Person {
    private String name;
    private int age;
    private String job;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Person(String name, int age, String job) {
        this.name = name;
        this.age = age;
        this.job = job;
    }

    @Override
    public String toString() {//重写toString方法来获取对象的属性
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", job='" + job + '\'' +
                '}';
    }
}
